//
//  UserAccountViewModel.swift
//  WeChat
//
//  Created by 梁迪 on 15/12/14.
//  Copyright © 2015年 梁迪. All rights reserved.
//

import UIKit
import AFNetworking

class UserAccountViewModel: NSObject {
    
    var userAccount: UserAccount?
    
    override init() {
        userAccount = UserAccount.loadAccount()
        super.init()
    }
    
    var token: String? {
        return userAccount?.access_token
    }
    
    var userLogin:Bool{
        return userAccount?.access_token != nil
        
        
    }
    //头像
    var headURL:NSURL?{
        let url = NSURL(string: userAccount?.avatar_large ?? "")
        return url
        
        
    }
    
    
    //用户名
    var userName:String?{
        
        return userAccount?.name
    }
    
    
    func loadAccessToken(code: String, finished:(isSuccess: Bool) ->()){
        
        let urlString = "oauth2/access_token"
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
        
        NetworkTools.sharedTools.requestJSONDict(.POST,urlString: urlString, parameters: parameters) { (dict, error) -> () in
            if error != nil {
               finished(isSuccess: false)
                return
                
            }
            
            let userAccount = UserAccount(dict: dict!)
            
            self.loadUserInfo(userAccount, finished: finished)
            
            
        }
        
        
        
        
//        let AFN = AFHTTPSessionManager()
//        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
//        AFN.POST(urlString, parameters: parameters, progress: { (p) -> Void in
//            //            print("pppppppppp--------\(p)")
//            }, success: { (_, result) -> Void in
//                //                print("result--------\(result)")
//                if let dict = result as? [String : AnyObject]{
//                    //                    let token = dict["access_token"] as! String
//                    //                    let uid = dict["uid"] as! String
//                    //                    self.loadUserInfo(token, uid: uid)
//                    
//                    //字典转模型
//                    let userAccount = UserAccount(dict: dict )
//                    //                    print("wwwwwwwww\(userAccount)")
//                    self.loadUserInfo(userAccount,finished: finished)
//                    
//                }
//                
//                
//            }) { (_, error) -> Void in
//                print("获取 token\(error)")
//                finished(isSuccess: false)
//        }
        
    }
    
    
    private func loadUserInfo(userAccount:UserAccount,finished:(isSuccess:Bool) -> ()){
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        if let token = userAccount.access_token, uid = userAccount.uid
            
        {
            
            let parameters = ["access_token":token,"uid":uid]
            
            NetworkTools.sharedTools.requestJSONDict(.GET, urlString: urlString, parameters: parameters, finished: { (dict, error) -> () in
                if error != nil{
                    
                  finished(isSuccess: false)
                    return
                }
                
                
                                        let name = dict!["name"] as!String
                                        let avatar_large = dict!["avatar_large"] as!String
                                        //                    print(name,avatar_large)
                
                                        userAccount.name = name
                                        userAccount.avatar_large = avatar_large
                                        userAccount.saveAccout()
                                        finished(isSuccess: true)
                
                
                
            })
            
            
//            let AFN = AFHTTPSessionManager()
//            AFN.GET(urlString, parameters: parameters, progress: { (p) -> Void in
//                //            print("pppppppppp--------\(p)")
//                }, success: { (_, result) -> Void in
//                    //                print("result--------\(result)")
//                    if let dict = result{
//                        
//                        let name = dict["name"] as!String
//                        let avatar_large = dict["avatar_large"] as!String
//                        //                    print(name,avatar_large)
//                        
//                        userAccount.name = name
//                        userAccount.avatar_large = avatar_large
//                        userAccount.saveAccout()
//                        finished(isSuccess: true)
//                    }
//                    
//                    
//                }) { (_, error) -> Void in
//                    print("获取用户信息\(error)")
//    finished(isSuccess: false)
//            }
            
            
            
            
            
        }
        
        
    }
    


}
